package com.standardoperationapp.operationplan;

import static android.text.InputType.TYPE_CLASS_NUMBER;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.standardoperationapp.R;
import com.standardoperationapp.activity.BluetoothSetActivity;
import com.standardoperationapp.bean.CheckLockEmpowerBean;
import com.standardoperationapp.bean.EmergencyDoorBean;
import com.standardoperationapp.bean.GunWenBean;
import com.standardoperationapp.global.GlobalVaries;
import com.standardoperationapp.global.GsonUtils;
import com.standardoperationapp.net.BaseSubscribe;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GuiWenActivity extends AppCompatActivity {

    private int planId;
    private int workPointId;
    private int id;
    private int implementId;
    private Spinner spinner;
    private List<String> list = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private String TAG="GuiWenActivity";
    private LinearLayout zuoyeqianLL;
    private LinearLayout zuoyezhongLL;
    private LinearLayout zuoyehouLL,youfengLL;
    private TextView titleTv;
    private String title;
    private Button sumbitBtn;
    private EditText zyqleft,zyqRight,zyzLeft,zyzRight,zyhLeft,zyhRight,yfLeft,yfRight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gui_wen);
        planId = getIntent().getIntExtra("planId", 0);
        workPointId = getIntent().getIntExtra("workPointId", 0);
        implementId =getIntent().getIntExtra("implementId", 0);
        id = getIntent().getIntExtra("id", 0);
        title = getIntent().getStringExtra("title");
        initSpinner();
        titleTv=(TextView)findViewById(R.id.guiwen_title) ;
        zuoyeqianLL =findViewById(R.id.guiwen_zuoyeqian);
        zuoyezhongLL =findViewById(R.id.guiwen_zuoyezhong);
        zuoyehouLL =findViewById(R.id.guiwen_zuoyehou);
        youfengLL =findViewById(R.id.guiwen_youfeng);
        titleTv.setText(title);
        initEditText();
        initButton();
    }

    private void initButton() {
        sumbitBtn =(Button) findViewById(R.id.guiwen_btn);
        sumbitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int type=0;
                if(spinner.getSelectedItem().toString().equals("有缝线路")){
                    if(yfLeft.getText().toString().isEmpty()
                            ||yfRight.getText().toString().isEmpty()){
                        Toast.makeText(GuiWenActivity.this, "数据不能为空！" , Toast.LENGTH_LONG).show();
                        return;
                    }
                    type =2;
                }else  if(spinner.getSelectedItem().toString().equals("无缝线路")) {
                    if (zyqleft.getText().toString().isEmpty()
                            || zyqRight.getText().toString().isEmpty()
                            || zyzLeft.getText().toString().isEmpty()
                            || zyzRight.getText().toString().isEmpty()
                            || zyhLeft.getText().toString().isEmpty()
                            || zyhRight.getText().toString().isEmpty()
                    ) {
                        Toast.makeText(GuiWenActivity.this, "数据不能为空！", Toast.LENGTH_LONG).show();
                        return;
                    }
                    type =1;
                }
                submitInfo(type);
            }
        });
    }

    private void initEditText() {
        zyqleft = (EditText) findViewById(R.id.guiwen_zyq_left);
        zyqRight = (EditText) findViewById(R.id.guiwen_zyq_right);
        zyzLeft = (EditText) findViewById(R.id.guiwen_zyz_left);
        zyzRight = (EditText) findViewById(R.id.guiwen_zyz_right);
        zyhLeft = (EditText) findViewById(R.id.guiwen_zyh_left);
        zyhRight =(EditText) findViewById(R.id.guiwen_zyh_right);
        yfLeft =(EditText) findViewById(R.id.guiwen_yf_left);
        yfRight =(EditText) findViewById(R.id.guiwen_yf_right);
        zyqleft.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        zyqRight.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        zyzLeft.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        zyzRight.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        zyhLeft.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        zyhRight.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        yfLeft.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);
        yfRight.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);

    }

    public void initSpinner() {
        spinner = (Spinner) findViewById(R.id.guiwen_sp);
        list.add("有缝线路");
        list.add("无缝线路");
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        // 第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 第四步：将适配器添加到下拉列表上
        spinner.setAdapter(adapter);
        // 第五步：为下拉列表设置各种事件的响应，这个事响应菜单被选中
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // TODO Auto-generated method stub
                /* 将所选mySpinner 的值带入myTextView 中 */
                //  Log.e(TAG,"您选择的是：" + adapter.getItem(arg2));
                /* 将mySpinner 显示 */
                arg0.setVisibility(View.VISIBLE);
                if (adapter.getItem(arg2).toString().equals("有缝线路")) {
                    youfengLL.setVisibility(View.VISIBLE);
                    zuoyehouLL.setVisibility(View.GONE);
                    zuoyeqianLL.setVisibility(View.GONE);
                    zuoyezhongLL.setVisibility(View.GONE);
                } else if (adapter.getItem(arg2).toString().equals("无缝线路")) {
                    youfengLL.setVisibility(View.GONE);
                    zuoyehouLL.setVisibility(View.VISIBLE);
                    zuoyeqianLL.setVisibility(View.VISIBLE);
                    zuoyezhongLL.setVisibility(View.VISIBLE);
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                arg0.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void submitInfo(int lineType){
        List<GunWenBean>list = new ArrayList<GunWenBean>();
        if(lineType==1){ //无缝线路
            GunWenBean gunWenBean = new GunWenBean(planId,implementId,lineType,1,Double.parseDouble(zyqleft.getText().toString()),Double.parseDouble(zyqRight.getText().toString()));
             list.add(gunWenBean);
            gunWenBean = new GunWenBean(planId,implementId,lineType,2,Double.parseDouble(zyzLeft.getText().toString()),Double.parseDouble(zyzRight.getText().toString()));
            list.add(gunWenBean);
            gunWenBean = new GunWenBean(planId,implementId,lineType,3,Double.parseDouble(zyhLeft.getText().toString()),Double.parseDouble(zyhRight.getText().toString()));
            list.add(gunWenBean);
        }else if(lineType==2){//有缝线路
            GunWenBean gunWenBean = new GunWenBean(planId,implementId,lineType,0,Double.parseDouble(yfLeft.getText().toString()),Double.parseDouble(yfRight.getText().toString()));
            list.add(gunWenBean);
        }
        BaseSubscribe.postTrackThermometerDate( list, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                EmergencyDoorBean resultBean = GsonUtils.fromJson(result.toString(), EmergencyDoorBean.class);
                if (resultBean.getCode() == 200) {

                    Toast.makeText(GuiWenActivity.this, "发送轨温仪信息成功！" , Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onFault(String errorMsg) {
                Toast.makeText(GuiWenActivity.this, "发送轨温仪信息失败！" + errorMsg, Toast.LENGTH_LONG).show();
            }

        }));
    }
}